package com.yanqu.road.dao.impl.activity.xiayue;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.xiayue.config.XiaYuePlaceConfig;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class XiaYuePlaceConfigDaoImpl extends TempDao {

    private static final String getXiaYuePlaceConfigSql = "select * from `t_s_activity_xiayue_place` where `activityId`=?";

    public Map<Integer, XiaYuePlaceConfig> getXiaYuePlaceConfigMap(int activityId) {
        Map<Integer, XiaYuePlaceConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getXiaYuePlaceConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                XiaYuePlaceConfig xiaYuePlaceConfig = new XiaYuePlaceConfig();
                xiaYuePlaceConfig.setActivityId(rs.getInt("activityId"));
                xiaYuePlaceConfig.setId(rs.getInt("id"));
                xiaYuePlaceConfig.setName(rs.getString("name"));
                xiaYuePlaceConfig.setDesc(rs.getString("desc"));
                xiaYuePlaceConfig.setWeight(rs.getInt("weight"));
                for (String animal : StringUtils.stringToStringList(rs.getString("animalList"), "\\|")) {
                    List<Integer> list = StringUtils.stringToIntegerList(animal, ",");
                    xiaYuePlaceConfig.getAnimalList().add(list.get(0));
                    xiaYuePlaceConfig.getWeightList().add(list.get(1));
                }
                map.put(xiaYuePlaceConfig.getId(), xiaYuePlaceConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

}
